package cn.ilikes.tools.hbase.jpa.configs;

import java.util.List;
import java.util.Properties;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.JPAHbasePersistenUnitEnv;
import cn.ilikes.tools.hbase.jpa.JPAHbaseSystemEnv;
import cn.ilikes.tools.hbase.jpa.clazzes.ClassFilter;
import cn.ilikes.tools.hbase.jpa.clazzes.IJPAHbaseFilterClassLoader;
import cn.ilikes.tools.hbase.jpa.clazzes.JPAHbaseSysClassLoader;
import cn.ilikes.tools.hbase.jpa.metadatas.DefultEntityMetaDataFactory;
import cn.ilikes.tools.hbase.jpa.metadatas.EntityMetaDataImpl;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaDataFactory;
import cn.ilikes.tools.hbase.jpa.metadatas.processor.TableProcessor;
import cn.ilikes.tools.hbase.jpa.metadatas.processor.TableProcessorException;
import cn.ilikes.tools.logs.Loggerfactory;

public class PersistenceIEntityMetaDataFactoryConfigure implements IConfigure {

	private static Logger logger = Loggerfactory.instance(PersistenceIEntityMetaDataFactoryConfigure.class);

	public PersistenceIEntityMetaDataFactoryConfigure(Properties configs) {
		Loggerfactory.info(logger, "load Persistence EntityMeta DataFactory  Configure ......");
	}

	@Override
	public void config(final String persistenceUnitsName, final JPAHbasePersistenUnitEnv hbasePersistenUnitEnv) throws JPAHBaseConfigurationException {
		Loggerfactory.info(logger, "start   process  persistence unit entity table ;  persistenceUnitsName  : " + persistenceUnitsName);
		final IEntityMetaDataFactory metaDataManagerFactory = new DefultEntityMetaDataFactory();
		if (hbasePersistenUnitEnv.gethBasePersistenceUnitMetadata().getClasses().size() == 0)
			throw new JPAHBaseConfigurationException("pu : " + persistenceUnitsName + "  <class> not contain   class ! ");

		IJPAHbaseFilterClassLoader hbaseSysClassLoader = new JPAHbaseSysClassLoader(hbasePersistenUnitEnv);
		{
			hbaseSysClassLoader.addJPAHbaseFilterClassLoader(new ClassFilter() {
				private List<String> clazzs = hbasePersistenUnitEnv.gethBasePersistenceUnitMetadata().getClasses();

				@Override
				public boolean isContain(String resourceName) {
					for (String clazz : clazzs) {
						if (resourceName.startsWith(clazz))
							return true;
					}
					return false;
				}

				@Override
				public void filter(String classname, Class<?> clazz) {
					Table table = clazz.getAnnotation(Table.class);
					if (table == null)
						return;
					if (table.schema() != null && table.schema().equals(persistenceUnitsName)) {
						if (clazz.isAnnotationPresent(Entity.class)) {
							try {
								IEntityMetaData entityMetaData = new EntityMetaDataImpl(persistenceUnitsName, clazz, hbasePersistenUnitEnv);
								TableProcessor tableProcessor = new TableProcessor();
								tableProcessor.process(clazz, entityMetaData);
								if (Loggerfactory.isdebug(logger))
									Loggerfactory.info(logger, "Entity Meta Data Factory load Entity Meta  class :" + clazz
											+ ";   entityMetaData  " + entityMetaData);
								metaDataManagerFactory.loadIEntityMetaDatas(clazz, entityMetaData);
							} catch (TableProcessorException e) {
								e.printStackTrace();
							}
						}
					} else {
						if (table.schema() == null) {
							//					JPAHBaseConfigRunTimeException hBaseConfigRunTimeException 
							//					              =new JPAHBaseConfigRunTimeException("clazz : "+clazz.getSimpleName()+"  @Table schema is null ; please set @Table schema ");
							Loggerfactory.warn(logger, "clazz : " + clazz.getSimpleName() + "  @Table schema is null ; please set @Table schema ");
						}
						if (table.schema().equals(persistenceUnitsName)) {
							//					JPAHBaseConfigRunTimeException hBaseConfigRunTimeException 
							//					              =new JPAHBaseConfigRunTimeException("clazz : "+clazz.getSimpleName()+"  @Table schema is null ; please set @Table schema ");
							Loggerfactory.warn(logger, "clazz : " + clazz.getSimpleName() + "  @Table schema  != " + persistenceUnitsName
									+ " ; please set @Table schema = pu name ");
						}
					}

				}

			});
			hbaseSysClassLoader.hbaseEntityFilter();
		}
		hbasePersistenUnitEnv.setEntityMetaDataFactory(metaDataManagerFactory);
	}

}
